package com.shuoyue.ycgk.ui.mycourse;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.Course;
import com.shuoyue.ycgk.ui.course.info.CourseInfoActivity;
import com.shuoyue.ycgk.ui.course.mycourse.CoursePauchesAdapter;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FragmentMyCoursePauches extends BaseMvpFragment<MyCourseContract.Presenter> implements MyCourseContract.View {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;
    @BindView(R.id.invilidatecourse)
    TextView invilidatecourse;
    CoursePauchesAdapter adapter;

    public static FragmentMyCoursePauches getInstance() {
        FragmentMyCoursePauches fragment = new FragmentMyCoursePauches();
        return fragment;
    }


    @Override
    public void initParams(Bundle bundle) {

    }

    @Override
    protected void initData() {
        mPresenter = new MyCourseContract.Presenter();
        mPresenter.attachView(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_course_pauches;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 12), true));
        adapter = new CoursePauchesAdapter(null);
        recyclerView.setAdapter(adapter);
        refreshlayout.setEnableLoadMore(false);

        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getMyCourse(1);
            refreshLayout.finishRefresh(2000);
        });

        adapter.setOnItemChildClickListener((adapter, view, position) -> {
            Course course = (Course) adapter.getItem(position);
            switch (view.getId()) {
                case R.id.lay_all:
                    startActivity(new Intent(mContext, CourseInfoActivity.class)
                            .putExtra("id", course.getId()));
                    break;
            }
        });
        registEmptyView(adapter);
        registEmptyView(adapter, R.layout.layout_empty_course);

    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getMyCourse(1);

    }

    @Override
    public void getMyCourseSuc(List<Course> courses) {
        adapter.resetData(courses);
        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }

    @Override
    public void getAllCourseSuc(ListWithPage<Course> courses) {

    }

    @Override
    public String getTitle() {
        return "已购课程";
    }

    @OnClick(R.id.invilidatecourse)
    public void onViewClicked() {
        startActivity(new Intent(mContext, OutDatedCourseActivity.class));
    }
}
